package com.glacier.yuyuan.gen.webapp.web;

import cn.hutool.core.util.StrUtil;
import com.glacier.common.core.domain.Page;
import com.glacier.common.core.domain.Result;
import com.glacier.common.core.domain.form.ListWrapper;
import com.glacier.yuyuan.gen.webapp.domain.GenDatasource;
import com.glacier.yuyuan.gen.webapp.service.GenDatasourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * date 2022-11-10 15:12
 * 数据源 控制层
 *
 * @author glacier
 * @version 1.0
 */
@RequestMapping("/genDatasource")
@Controller
public class GenDatasourceController extends BaseController {

    private GenDatasourceService genDatasourceService;

    @Autowired
    public void setGenDatasourceService(GenDatasourceService genDatasourceService) {
        this.genDatasourceService = genDatasourceService;
    }

    @GetMapping("")
    public String list() {
        return "view/genDatasource/genDatasourceList";
    }

    @ResponseBody
    @PostMapping("/page")
    public Result<List<GenDatasource>> page(Page<GenDatasource> page, GenDatasource genDatasource) {
        page = genDatasourceService.findPage(page, genDatasource);
        return Result.ok("ok", page.getData(), page.getTotal());
    }

    @GetMapping("/form")
    public String form(GenDatasource genDatasource, Model model) {
        if (StrUtil.isNotBlank(genDatasource.getId())) {
            genDatasource = genDatasourceService.findById(genDatasource.getId());
        }
        model.addAttribute("entity", genDatasource);
        return "view/genDatasource/genDatasourceForm";
    }

    @PostMapping("/save")
    @ResponseBody
    public Result<Integer> save(GenDatasource genDatasource) {
        return Result.ok("操作成功！", genDatasourceService.save(genDatasource));
    }

    @ResponseBody
    @PostMapping("/del")
    public Result<String> del(ListWrapper<String> form) {
        genDatasourceService.batchDelete(form.getDatas());
        return Result.ok("操作成功", "");
    }

    /**
     * 探查表
     *
     * @param sourceId
     * @return
     */
    @ResponseBody
    @GetMapping("/expedition/{sourceId}")
    public Result<List<String>> expedition(@PathVariable("sourceId") String sourceId) {
        List<String> tables = genDatasourceService.getTables(sourceId);
        return Result.ok("ok", tables, tables.size());
    }
}
